package ru.volgogradetzzz.utils 
{
	//----------------------------------------------
	// Imports
	//----------------------------------------------
	
	import flash.display.Graphics;
	import ru.volgogradetzzz.geom.VLine;
	import ru.volgogradetzzz.math.VMatrix2x2;
	import ru.volgogradetzzz.math.VVector2;
	
	/**
	 * @author Volgogradetzzz
	 */
	public class DrawUtil 
	{
		//----------------------------------------------
		// Private fields
		//----------------------------------------------
		
		private var _graphics:Graphics;
		private var _headPos:VVector2 = new VVector2();
		private var _arrow1:VVector2 = new VVector2( -8, 2);
		private var _arrow2:VVector2 = new VVector2( 0, -4);
		private var _arrow3:VVector2 = new VVector2( 8, 2);
		
		//----------------------------------------------
		// Constructor
		//----------------------------------------------
		
		public function DrawUtil(g:Graphics) 
		{
			_graphics = g;
		}
		
		//----------------------------------------------
		// Public methods
		//----------------------------------------------
		
		public function setOrigin(v:VVector2):void
		{
			_headPos.x = v.x;
			_headPos.y = v.y;
		}
		
		public function drawNextVector(v:VVector2, showArrows:Boolean = true, showNormal:Boolean = false, color:uint = 0x000000):void
		{
			_graphics.lineStyle(1, color, 1, true);
			
			_graphics.moveTo(_headPos.x, _headPos.y);
			_graphics.lineTo(_headPos.x + v.x, _headPos.y + v.y);
			
			if (showNormal)
			{
				drawNormal(v, color, showArrows);
			}
			
			_headPos.add(v);
			
			if (showArrows)
			{
				drawArrow(_headPos, v, color);
			}
			
			_graphics.lineStyle();
		}
		
		public function drawPoint(v:VVector2, radius:Number = 2, color:uint = 0xFF0000):void
		{
			_graphics.beginFill(color);
			_graphics.drawCircle(v.x, v.y, radius);
			_graphics.endFill();
		}
		
		public function drawLine(line:VLine, showArrows:Boolean = true, showNormal:Boolean = false, color:uint = 0x000000):void
		{
			setOrigin(line.position);
			drawNextVector(line.direction, showArrows, showNormal, color);
		}
		
		public function clear():void
		{
			_graphics.clear();
		}
		
		//----------------------------------------------
		// Private methods
		//----------------------------------------------
		
		private function drawNormal(v:VVector2, color:uint, showArrows:Boolean):void
		{
			var normOrigin:VVector2 = v.scaleReturn(0.5);
			normOrigin.add(_headPos);
			var normal:VVector2 = v.normal();
			normal.normalize();
			normal.scale(20);
			
			_graphics.moveTo(normOrigin.x, normOrigin.y);
			_graphics.lineTo(normOrigin.x + normal.x, normOrigin.y + normal.y);
			
			if (showArrows)
			{
				drawArrow(normOrigin.addReturn(normal), normal, color);
			}
		}
		
		private function drawArrow(origin:VVector2, v:VVector2, color:uint):void
		{
			var ang:Number = Math.atan2(v.y, v.x);
			var m:VMatrix2x2 = new VMatrix2x2();
			m.prepareForRotation(ang);
			var a:VVector2 = m.multiplyByVector(_arrow1);
			var b:VVector2 = m.multiplyByVector(_arrow2);
			var c:VVector2 = m.multiplyByVector(_arrow3);
			
			_graphics.beginFill(color);
			_graphics.moveTo(origin.x, origin.y);
			_graphics.lineTo(origin.x + a.x, origin.y + a.y);
			_graphics.lineTo(origin.x + a.x + b.x, origin.y + a.y + b.y);
			_graphics.lineTo(origin.x + a.x + b.x + c.x, origin.y + a.y + b.y + c.y);
			_graphics.endFill();
		}
	}
}